package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    @Autowired
    private SpecParamService specParamService;
    /**
     * 根据分类id查询规格组集合
     * @param id 分类id
     * @return 规格组集合
     */
    @Override
    public List<SpecGroupDTO> listSpecGroupByCategoryId(Long id) {
        List<SpecGroup> specGroupList = this.list(new QueryWrapper<SpecGroup>().eq("category_id", id));
        return SpecGroupDTO.convertEntityList(specGroupList);
    }

    @Override
    public void saveSpecGroup(SpecGroupDTO specGroupDTO) {
        this.save(specGroupDTO.toEntity(SpecGroup.class));
    }

    @Override
    public void updateSpecGroup(SpecGroupDTO specGroupDTO) {
        this.updateById(specGroupDTO.toEntity(SpecGroup.class));
    }

    /**
     * 根据分类id 查询规格组集合 以及组内对应规格参数集合
     * @param cid 分类id
     * @return
     */
    @Override
    public List<SpecGroupDTO> listGroupAndParamByCid(Long cid) {
       /* //根据分类id找到对应的规格组
        List<SpecGroup> groupList = this.list(new QueryWrapper<SpecGroup>().eq("category_id", cid));
        List<SpecGroupDTO> specGroupDTOList = SpecGroupDTO.convertEntityList(groupList);
        if(CollectionUtils.isEmpty(groupList)){
            throw new LyException(404, "该分类下的规格组不存在！");
        }

        List<SpecGroupDTO> specGroupDTOList1 = specGroupDTOList.stream()
                //获取所有组id
                .map(SpecGroupDTO::getId)
                .map((gid) -> {
                    //根据组id找到所有规格参数集合
                    List<SpecParam> paramList = specParamService.list(new QueryWrapper<SpecParam>().eq("group_id", gid));

                    SpecGroupDTO specGroupDTO = new SpecGroupDTO();
                    //将获取到的集合注入specGroupDTO对象
                    specGroupDTO.setParams(SpecParamDTO.convertEntityList(paramList));
                    //注入对应组id
                    specGroupDTO.setId(gid);
                    return specGroupDTO;
                }).collect(Collectors.toList());

        //根据两个集合的组id相等，设置param属性
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            for (SpecGroupDTO groupDTO : specGroupDTOList1) {
                if(specGroupDTO.getId()==groupDTO.getId()){
                    specGroupDTO.setParams(groupDTO.getParams());
                    break;
                }
            }
        }

        return specGroupDTOList;*/
        //根据分类id查询对应的规格组集合
        List<SpecGroupDTO> specGroupDTOList = this.listSpecGroupByCategoryId(cid);

        //根据分类id查询所有的规格参数
        List<SpecParamDTO> specParamDTOList = specParamService.ListSpecParamsByCondition(cid, null, null);
        //将规格参数根据规格组分组，key为规格组id ,value为规格参数集合
        Map<Long, List<SpecParamDTO>> map = specParamDTOList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        specGroupDTOList.forEach(specGroupDTO -> specGroupDTO.setParams(map.get(specGroupDTO.getId())));
        return specGroupDTOList;

    }


}